﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Gollie.Mail;
using NLog;

namespace Gollie
{
	public class Global : System.Web.HttpApplication
	{
		protected static Logger logger = LogManager.GetCurrentClassLogger();

		internal static void HackAttempt()
		{
			logger.Fatal("Hack attempt seems to be going on. What to do...");
			logger.Info("For now, just sleep for one second");

			System.Threading.Thread.Sleep(1000);
		}

		protected void Application_Start(object sender, EventArgs e)
		{
			System.Globalization.CultureInfo.DefaultThreadCurrentCulture = new System.Globalization.CultureInfo("nl-NL");

			logger.Info("Application start");

			CacheConfig.Init();
		}

		protected void Session_Start(object sender, EventArgs e)
		{
			logger.Info("Session start, id {0}", Session.SessionID);
		}

		protected void Application_BeginRequest(object sender, EventArgs e)
		{

		}

		protected void Application_AuthenticateRequest(object sender, EventArgs e)
		{

		}

		protected void Application_Error(object sender, EventArgs e)
		{
			Exception exc = Server.GetLastError();
			logger.FatalException("Application Error: ", exc);

			Response.Write("<h2>Global Page Error</h2>\n");
			Response.Write(
				"<p>" + exc.Message + "</p>\n");
			Response.Write("Terug naar <a href='Default.aspx'>Home page</a>");

			// Clear the error from the server
/*
			//prevent exception-mail caused by links to old gollie-annuiteiten site.
			if (exc.Message.Contains("Calc.aspx") == false)
			{
				MailClient.Queue(new EMail
				{
					Name = "applicationerror",
					Recipients = "administrator@gollie.nl",
					Args = new Dictionary<string, string>()
				{
											{ "{timestamp}", DateTime.Now.ToString() },
											{ "{exception}", exc.Message },
										    
				}
				});
			}
*/
			Server.ClearError();
		}

		protected void Session_End(object sender, EventArgs e)
		{
			logger.Info("Session end, id {0}", Session.SessionID);
		}

		protected void Application_End(object sender, EventArgs e)
		{
			logger.Info("Appliction end");
		}
	}
}